package com.ship.dispatch.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ship.common.core.domain.SpPortData;
import com.ship.common.core.exception.BaseException;
import com.ship.common.core.exception.ServiceException;
import com.ship.common.core.utils.BeanCopyUtils;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.business.service.impl.BaseServiceImpl;
import com.ship.common.core.utils.StringUtil;
import com.ship.dispatch.bean.*;
import com.ship.dispatch.bo.SpYearPlanSchemeDetailBO;
import com.ship.dispatch.bo.VirtualShipOptimizeSchemeDetailBO;
import com.ship.dispatch.bo.VirtualShipSchemeDetailBO;
import com.ship.dispatch.mapper.SpPortInformationMapper;
import com.ship.dispatch.mapper.SpShipScheduleMapper;
import com.ship.dispatch.mapper.SpYearPlanSchemeDetailMapper;
import com.ship.dispatch.model.Vo.ShipLoadDaysAndRouteVo;
import com.ship.dispatch.model.Vo.ShipLoadDaysVo;
import com.ship.dispatch.service.SpShipScheduleService;
import com.ship.dispatch.service.SpYearPlanSchemeDetailService;
import com.ship.dispatch.util.CommonUtil;
import com.ship.dispatch.util.IDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 * 方案详情 服务实现类
 * </p>
 *
 * @author AbyssRabbit@163.com
 * @since 2023-10-17
 */
@Service
public class SpYearPlanSchemeDetailServiceImpl extends BaseServiceImpl<SpYearPlanSchemeDetailMapper, SpYearPlanSchemeDetail> implements SpYearPlanSchemeDetailService {

    @Autowired
    private SpShipScheduleMapper shipScheduleMapper;

    @Autowired
    private SpPortInformationMapper portInformationMapper;

    @Override
    public void deleteSpYearPlanSchemeDetail(Long schemeDetailId) {
        SpYearPlanSchemeDetail spYearPlanSchemeDetail = this.getBaseMapper().selectById(schemeDetailId);
        if(spYearPlanSchemeDetail != null){
            this.getBaseMapper().deleteById(spYearPlanSchemeDetail);
        }
    }

    /**
     * 虚拟船保存甘特图(年度计划)
     * @param virtualShipSchemeDetailBO
     * @return
     */
    @Override
    public SpYearPlanSchemeDetail saveVirtualShipSchemeDetail(VirtualShipSchemeDetailBO virtualShipSchemeDetailBO){
        //数字要以最终下发的计划中预计装载时间顺序排序自增
        SpYearPlanSchemeDetail schemeDetail = new SpYearPlanSchemeDetail();
        schemeDetail.setCreateBy(virtualShipSchemeDetailBO.getCreateBy());
        schemeDetail.setId(IDUtil.nextId());
        schemeDetail.setSchemeId(virtualShipSchemeDetailBO.getSchemeId());
        schemeDetail.setResourceContract(virtualShipSchemeDetailBO.getConId());
        schemeDetail.setContractName(virtualShipSchemeDetailBO.getContractName());
        schemeDetail.setSchemeNumber(virtualShipSchemeDetailBO.getNum());
        schemeDetail.setSeller(virtualShipSchemeDetailBO.getSeller());
        schemeDetail.setBuyer(virtualShipSchemeDetailBO.getBuyer());
        schemeDetail.setExpectedLoadingPort(virtualShipSchemeDetailBO.getLoadingPort());
        schemeDetail.setExpectedUnloadingPort(virtualShipSchemeDetailBO.getUnloadingPort());
        schemeDetail.setTradeType(virtualShipSchemeDetailBO.getTradeType());
        schemeDetail.setTradeBody(virtualShipSchemeDetailBO.getTradeBody());
        schemeDetail.setGoodsNum(schemeDetail.getSeller() + "-FOB-" + virtualShipSchemeDetailBO.getNum());
//        schemeDetail.setShipRoute(shipRoute);

        //date:排列出来的装载时间
        //date = DateUtil.format(DateUtil.parse( "2024-04-13 00:00:00", "yyyy-MM-dd HH:mm:ss"), "yyyy-MM-dd HH:mm:ss");

        Date parse = DateUtil.parse(virtualShipSchemeDetailBO.getDate(), "yyyy-MM-dd HH:mm:ss");
        Integer year = DateUtil.year(new Date()) + 1;
        schemeDetail.setVoyageNo("CNOOC-" + year + "-" + virtualShipSchemeDetailBO.getNum());

        //处理虚拟船航线
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(parse);
        int month = calendar.get(Calendar.MONTH) + 1;
        if(month>=4 && month<=10){
            schemeDetail.setShipRoute("1");
        }
        if (month <= 3 || month >= 11) {
            schemeDetail.setShipRoute("2");
        }
        schemeDetail.setExpectedLoadingDate(parse);
        schemeDetail.setExpectedLoadingCompletionWindow(DateUtil.offsetDay(parse, 2));
        schemeDetail.setShipName(virtualShipSchemeDetailBO.getShipName());
        schemeDetail.setImo(virtualShipSchemeDetailBO.getImo());
        Date loadingDate = schemeDetail.getExpectedLoadingDate();
        //中国预计卸载时间(走巴拿马和不走巴拿马)
        schemeDetail.setExpectedUnloadingWindow(DateUtil.offsetDay(loadingDate, virtualShipSchemeDetailBO.getVoyageDays()));
        //印度预计卸载时间
        schemeDetail.setIndiaUnloadTime(DateUtil.offsetDay(loadingDate, virtualShipSchemeDetailBO.getIndiaDays()));
        //欧洲预计卸载时间
        schemeDetail.setEuropeUnloadTime(DateUtil.offsetDay(loadingDate, virtualShipSchemeDetailBO.getEuropeDays()));
        //预计满载航程天数
        schemeDetail.setExpectedFullLoadVoyageDays(virtualShipSchemeDetailBO.getVoyageDays());
        //印度空载航程天数
        schemeDetail.setIndiaEmptyLoadDays(virtualShipSchemeDetailBO.getIndiaDays());
        //欧洲空载航程天数
        schemeDetail.setEuropeEmptyLoadDays(virtualShipSchemeDetailBO.getEuropeDays());
        //预计卸载窗口
        Date unload = schemeDetail.getExpectedUnloadingWindow();
        //预计卸载完成窗口
        schemeDetail.setExpectedUnloadingCompletionWindow(DateUtil.offsetDay(unload, 2));
        //预计空载航程天数
        schemeDetail.setExpectedEmptyLoadVoyageDays(virtualShipSchemeDetailBO.getVoyageDays());
        //预计卸载完成窗口
        Date unloadCom = schemeDetail.getExpectedUnloadingCompletionWindow();
        //预计空载返程窗口
        schemeDetail.setExpectedEmptyReturnWindow(DateUtil.offsetDay(schemeDetail.getExpectedUnloadingCompletionWindow(), virtualShipSchemeDetailBO.getVoyageDays()));
        schemeDetail.setCreateDate(new Date());
        schemeDetail.setSingleGoodsQuantity(virtualShipSchemeDetailBO.getSingleGoodsQuantity());
        schemeDetail.setShipSpeed(virtualShipSchemeDetailBO.getShipSpeed());
//        schemeDetail.setQuantityUnit(schemeModel.getQuantityUnit());
        schemeDetail.setPlanId(virtualShipSchemeDetailBO.getPlanId());
        schemeDetail.setOilConsumption(virtualShipSchemeDetailBO.getOilConsumption());

        int numSave = this.getBaseMapper().insert(schemeDetail);
        if (numSave > 0) {
            //保存船舶班期表
            SpShipSchedule spShipSchedule = new SpShipSchedule();
            BeanUtils.copyProperties(schemeDetail, spShipSchedule);
            spShipSchedule.setId(IDUtil.nextId());
            spShipSchedule.setStartingDateOfLeaseBegin(virtualShipSchemeDetailBO.getLeaseTime());
            Date redeliveryDateEnd = DateUtil.offsetDay(virtualShipSchemeDetailBO.getLeaseTime(),virtualShipSchemeDetailBO.getLeaseDuration());
            spShipSchedule.setRedeliveryDateEnd(redeliveryDateEnd);
            shipScheduleMapper.insert(spShipSchedule);
        }

        List<SpYearPlanSchemeDetail> yearPlanSchemeDetails = this.baseMapper.selectList(
                new LambdaQueryWrapper<SpYearPlanSchemeDetail>().eq(SpYearPlanSchemeDetail::getSchemeId, schemeDetail.getSchemeId()));
        if (yearPlanSchemeDetails.size() > 0) {
            yearPlanSchemeDetails = CommonUtil.resetVoyageNoAndGoodsNumber(yearPlanSchemeDetails);
            this.updateBatchById(yearPlanSchemeDetails);
        }

        return schemeDetail;
    }

    @Override
    public SpYearPlanSchemeDetail selectMaxSchemeDetail(String createBy, Long planId) {
        return this.getBaseMapper().selectMaxSchemeDetail(createBy,planId);
    }

}
